package com.chen.io;

import java.io.File;
import java.util.LinkedList;

/**
 * <p>
 * RemoveCVS.java
 * </p>
 * <p>
 * Created on Apr 20, 2009, 9:01:19 AM
 * </p>
 * <p>
 * Copyright (c) 2007-2009. CUCKOO Workgroup, USTC, P.R.China
 * </p>
 * 
 * @author Ren Jian
 */
public class RemoveCVS {
	
	private static String REMOVE_SUFFIX = "target";
	
	public RemoveCVS(String srcFolderPath) {
		LinkedList folderList = new LinkedList();
		File firstLevel = new File(srcFolderPath);
		File fileList[] = firstLevel.listFiles();
		for (int i = 0; i < fileList.length; i++) {
			if (fileList[i].isDirectory()) {
				if (!fileList[i].getName().equals(REMOVE_SUFFIX)) {
					folderList.add(fileList[i]);
				} else {
					removeDir(fileList[i]);
				}
			} else {
				System.out.println(fileList[i].getAbsolutePath());
			}
		}
		File tempFile;
		while (!folderList.isEmpty()) {
			tempFile = (File) folderList.removeFirst();
			if (tempFile.isDirectory()) {
				fileList = tempFile.listFiles();
				if (fileList == null) {
					continue;
				}
				for (int i = 0; i < fileList.length; i++) {
					if (fileList[i].isDirectory()) {
						if (!fileList[i].getName().equals(REMOVE_SUFFIX)) {
							folderList.add(fileList[i]);
						} else {
							removeDir(fileList[i]);
						}
					} else {
						System.out.println(fileList[i].getAbsolutePath());
					}
				}
			} else {
				System.out.println(tempFile.getAbsolutePath());
			}
		}
	}

	private void removeDir(File cvsFolder) {
		File[] fileList = cvsFolder.listFiles();
		for (int i = 0; i < fileList.length; i++) {
			File file = fileList[i];
			if (file.isDirectory()) {
				removeDir(file);
			} else {
				file.delete();
			}
		}
		cvsFolder.delete();
	}
	
	private void removefile(File file) {
		if(file !=null) {
			file.delete();
		}
	}

	public static void main(String[] args) {
		new RemoveCVS("/Users/jay/资料/longshine");
//		removeFileByPath("/Users/jay/资料/longshine/saas_new/纯净版工程/invest/app/service");
	}
	
	private static void removeFileByPath(String srcFolderPath) {
		File firstLevel = new File(srcFolderPath);
		removeFile(firstLevel);
	}
	
	private static String file_name=".project";
	
	private static void removeFile(File file) {
		File fileList[] = file.listFiles();
		for (int i = 0; i < fileList.length; i++) {
			if (fileList[i].isDirectory()) {
				//removeFile(fileList[i]);
			} 
//			else {
//				if(fileList[i].getName().equals(file_name)) {
//					fileList[i].delete();
//					System.out.println(fileList[i].getAbsolutePath());
//				}
//			}
		}
	}
}
